<?php

/**
 * 自动生成 MerUserController
 * 
 * Apps/Mp/Controller/MerUserController.class.php
 * Zix @ 2015-06-15
 */

namespace Mp\Controller;

use Common\Service\MerUserService ;
use Common\Service\MerDictionaryService ;

class MerUserController extends MpController {

  public function index() {
    $action = I('get.action', 'read');
    $MerUser = MerUserService::instance();
    $data = $this->_init_data();
    
    $pid = I('get.pid' , '' );
    
    if ( $pid === '' ) {
      $data['module_name'] = '用户';
    } else {
      $puser_data = $MerUser->get_by_id($pid);
      $data['module_name'] = $puser_data['nickname'] . ' 推广的用户';
    }
    
    $data['module_url'] = U($this->base_path . 'index') ;

    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id,
      'mer_id' => $this->mer_id ,
      'module_url' => U( $this->base_path . 'index' ) ,
      'read_url'   => U( $this->base_path . 'read'  ) . "?pid=$pid",
      'create_url' => U( $this->base_path . 'create') ,
      'update_url' => U( $this->base_path . 'update') ,
      'delete_url' => U( $this->base_path . 'delete') ,
      'create_wx_qrcode_url' => U( $this->base_path . 'create_wx_qrcode' ),
      'user_promote_url' => U('Mp/MerUserPromoteStat/index') . '/',
      //''
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $MerUser->get_default_row( ) 
    );

    $data['area'] = array(
      'get_default_url'     => U( $this->module . '/area/get_default'),
      'get_by_pid_url'      => U( $this->module . '/area/get_by_pid'),
      'get_by_district_url' => U( $this->module . '/area/get_by_district'),
    );
    
    $MerDictionary = MerDictionaryService::instance();
    $data['param'] = array(
      'role' => $MerDictionary->get_user_role( $this->mer_id ),
      'status' => $MerDictionary->get_user_status( $this->mer_id ),
      'provider' => $MerUser->provider ,
    );

    $data['config']['row']['role_id'] = key($data['param']['role']);;
    $data['config']['row']['status'] = key($data['param']['status']);

    $this->display_with_layout($data, $this->controller . ':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'keyword'   => $post['keyword'] ,
      'pid'       => I('get.pid' , ''),
      'page'      => $post['page'],
      'page_size' => $post['page_size'],
      'mer_id'    => $post['mer_id'],
      'promote_num' => TRUE ,
      'sort'  => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $MerUser = MerUserService::instance();
    $data['rows'] = $MerUser->get_by_cond($config);

    //print_r($data);
    $config['count'] = TRUE;
    $data['total'] = $MerUser->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $post['password'] = trim($post['password']);
    if ( empty( $post['password'] ) ) {
      $this->ajaxReturn(ajax_arr('请填写密码'));
    }
    
    if ( empty( $post['binding_date'] ) ) {
      unset($post['binding_date']);
    }
    
    $post['password'] = md5($post['password']);
    $post['mer_id'] = $this->mer_id;
    $MerUser = MerUserService::instance();
    $ret = $MerUser->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $post['password'] = trim($post['password']);
    if ( empty( $post['password'] ) ) {
      unset($post['password']);
    } else {
      $post['password'] = md5($post['password']);
    }
    
    if ( empty( $post['binding_date'] ) ) {
      unset($post['binding_date']);
    }
    
    $MerUser = MerUserService::instance();
    $ret = $MerUser->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $MerUser = MerUserService::instance();
    $ret = $MerUser->delete($ids); 
    $this->ajaxReturn($ret);
  }
  
  function create_wx_qrcode() {
    $post = I('post.');
    
    //取appid 等
    $MerUser = MerUserService::instance();
    $ret = $MerUser->create_wx_qrcode($this->mer_id , $post['wx_id'] , $post['user_id']);
    $this->ajaxReturn($ret);
  }
}
